#pragma once
#include "queue.h"

void QInit(Queue* pq)
{
	assert(pq);

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void QDestory(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void QPush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	newnode->data = x;
	newnode->next = NULL;
	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

void QPop(Queue* pq)
{
	assert(pq);
	assert(!QEmpty(pq));
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		QNode* cur = pq->phead->next;
		free(pq->phead);
		pq->phead = cur;
	}
	pq->size--;
}

QDataType QBack(Queue* pq)
{
	assert(pq);
	assert(!QEmpty(pq));
	return pq->ptail->data;
}

QDataType QFront(Queue* pq)
{
	assert(pq);
	assert(!QEmpty(pq));
	return pq->phead->data;
}

int QSize(Queue* pq)
{
	return pq->size;
}

bool QEmpty(Queue* pq)
{
	assert(pq);
	if (pq->size == 0)
		return true;
	return false;
}
